﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;

namespace TireService.Business.Portal.CustomerView
{
    public partial class TireSetList : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            // TEST parameter
            //Session["CustomerId"] = "10000";

            // Bind car data to gridview
            BindTireSet(db, Convert.ToInt32(Session["CustomerId"]));

            // Change gridview pagesize based on dropdownlist
            gvTireSet.SettingsPager.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);

            // Dispose connection to database
            db.Dispose();
        }

        #region Private Functions

        /// <summary>
        /// Function that bind tireset data to gridview
        /// </summary>
        /// <param name="db">Connection to database</param>
        /// <param name="CustomerId">Id on customer</param>
        private void BindTireSet(TireServiceDB db, int CustomerId)
        {
            gvTireSet.DataSource = db.GetCustomerTireSets(CustomerId, true).Where(p => p.CargoRoomNumber != null).ToList();
            gvTireSet.DataBind();
        }

        /// <summary>
        /// Function that change grouping in order gridview
        /// </summary>
        /// <param name="ViewIndex">Integer setting the grouping mode</param>
        protected void GroupOrderData(int ViewIndex)
        {
            gvTireSet.ClearSort();
            gvTireSet.GroupSummary.Clear();
            switch (ViewIndex)
            {
                case 0:
                    // Nothing - clear grouping
                    gvTireSet.Columns["Name"].Visible = false;

                    break;
                case 1:
                    gvTireSet.Columns["Name"].Visible = false;
                    gvTireSet.GroupBy(gvTireSet.Columns["TireBrandDescription"]);

                    break;
                case 2:
                    gvTireSet.Columns["Name"].Visible = false;
                    gvTireSet.GroupBy(gvTireSet.Columns["CarBrandDescription"]);

                    break;
                case 3:
                    gvTireSet.Columns["Name"].Visible = false;
                    gvTireSet.GroupBy(gvTireSet.Columns["TireSetStatusDescription"]);

                    break;
                case 4:
                    gvTireSet.Columns["Name"].Visible = true;
                    gvTireSet.GroupBy(gvTireSet.Columns["Name"]);

                    break;
                //case 2:
                //    gvOrder.GroupBy(gvOrder.Columns["CustomerNumber"]);

                //    break;
                //case 3:
                //    gvOrder.GroupBy(gvOrder.Columns["PhotoDate"]);
                //    gvOrder.GroupBy(gvOrder.Columns["CustomerNumber"]);

                //break;
            }
            gvTireSet.ExpandAll();
        }

        #endregion

        #region Control Functions

        /// <summary>
        /// Button function for export of gridview
        /// </summary>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (ddlExport.Value == "0")
            {
                gvExport.WritePdfToResponse();
            }

            if (ddlExport.Value == "1")
            {
                gvExport.WriteXlsToResponse();
            }

            if (ddlExport.Value == "2")
            {
                gvExport.WriteCsvToResponse();
            }

            if (ddlExport.Value == "3")
            {
                gvExport.WriteRtfToResponse();
            }
        }

        /// <summary>
        /// Dropdown list function that change gridview grouping when selected item is changed
        /// </summary>
        protected void ddlView_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Group data
            GroupOrderData(Convert.ToInt32(ddlView.SelectedValue));
        }

        #endregion

    }
}